package com.gm.controller.cardOper;

import com.gm.model.Base.Const;
import com.gm.model.po.Bill;
import com.gm.model.po.Card;
import com.gm.model.po.CardOperPlan;
import com.gm.model.vo.ResMsg;
import com.gm.service.user.BillService;
import com.gm.service.user.CardOperService;
import com.gm.tools.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;

/**
 * Created by taojie on 17-8-13.
 */
@Controller
@RequestMapping("cardOper")
public class CardOperController {

    private Logger logger = LoggerFactory.getLogger(CardOperController.class);

    @Autowired
    private BillService billService;
    @Autowired
    private CardOperService cardOperService;

    @RequestMapping("gotoCardOperList.web")
    public ModelAndView cardOperList(Integer blid){
        ModelAndView view= new ModelAndView();
        List<CardOperPlan> cardOperPlanList= cardOperService.getCardOperListByBlid(blid);

        float consume = 0;
        float repayment = 0;

        for (int i= 0; i<cardOperPlanList.size(); i++){
            if(cardOperPlanList.get(i).getType() == -1){
                consume = consume + cardOperPlanList.get(i).getOperMoney();
            }else{
                repayment = repayment + cardOperPlanList.get(i).getOperMoney();
            }

        }

        Bill bill = billService.getBillById(blid);

        view.addObject("cardOperList", cardOperPlanList);
        view.addObject("consume", consume);
        view.addObject("repayment", repayment);
        view.addObject("bill", bill);
        view.setViewName("/cardOper/cardOper.jsp");
        return view;
    }

    @RequestMapping(value = "gotoAddCardOper.web")
    public ModelAndView gotoAddCardOper(Integer blid){
        ModelAndView view= new ModelAndView();
        view.addObject("blid", blid);
        view.setViewName("/cardOper/addCardOper.jsp");
        return view;
    }

    @RequestMapping(value = "addCardOper.do", method = RequestMethod.POST)
    @ResponseBody
    public ResMsg addCardOper(Integer blid, Integer type,  float operMoney, String operTime, Integer operator){
        logger.info("Method[addCardOper],plid[{}], type[{}], operMoney[{}], operTime[{}]",blid,type,operMoney,operTime);
        CardOperPlan cardOperPlan = new CardOperPlan();
        cardOperPlan.setBlid(blid);
        cardOperPlan.setType(type);
        cardOperPlan.setOperMoney(operMoney);
        cardOperPlan.setOperator(operator);
        cardOperPlan.setOperTime(Long.parseLong(operTime));
        cardOperPlan.setStatus(0);
        cardOperPlan.setCreateTime(DateUtils.getCurrentUnixTime());
        cardOperPlan.setUpdateTime(DateUtils.getCurrentUnixTime());

        ResMsg resMsg = new ResMsg();
        logger.info("Method[addCardOper],insert");
        int ret = cardOperService.insert(cardOperPlan);

        resMsg.setStatus(Const.RES_OK);


        logger.info("Method[Card],ret[{}]", ret);

        logger.info("Method[Card],resMsg[{}]", resMsg.toString());
        return resMsg;
    }

    @RequestMapping(value = "gotoUpdateCardOper.web")
    public ModelAndView gotoUpdateCardOper(Integer cardOperId){
        ModelAndView view= new ModelAndView();
        CardOperPlan cardOperPlan = cardOperService.getCardOperById(cardOperId);
        view.addObject("cardOperPlan", cardOperPlan);
        view.setViewName("/cardOper/updateCardOper.jsp");
        return view;
    }

    @RequestMapping(value = "updateCardOper.do", method = RequestMethod.POST)
    @ResponseBody
    public ResMsg updateCardOper(Integer id, Integer type,  float operMoney, String operTime, Integer operator){
        logger.info("Method[updateCardOper],cardOperId[{}], type[{}], operMoney[{}], operTime[{}]",id,type,operMoney,operTime);
        ResMsg resMsg = new ResMsg();
        CardOperPlan cardOperPlan = cardOperService.getCardOperById(id);
        if(cardOperPlan == null){
            resMsg.setError("排程不存在!");
        }else{
            cardOperPlan.setType(type);
            cardOperPlan.setOperMoney(operMoney);
            cardOperPlan.setOperator(operator);
            cardOperPlan.setOperTime(Long.parseLong(operTime));
            cardOperPlan.setUpdateTime(DateUtils.getCurrentUnixTime());
            int ret = cardOperService.update(cardOperPlan);
            resMsg.setStatus(Const.RES_OK);
        }
        return resMsg;
    }




    @RequestMapping(value = "cardOperDeal.do", method = RequestMethod.GET)
    @ResponseBody
    public ResMsg cardOperDeal(Integer id){
        logger.info("Method[cardOperDeal],id[{}]",id);
        ResMsg resMsg= new ResMsg();
        CardOperPlan cardOperPlan = cardOperService.getCardOperById(id);
        cardOperPlan.setStatus(1);
        cardOperService.update(cardOperPlan);

        resMsg.setStatus(Const.RES_OK);

        return resMsg;
    }

}
